import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class GetAreaService {

  constructor(private http: HttpClient) { }

  public getArea() {
    return new Promise((resolve, reject) => {
      const areaList = [];
      this.http.get('assets/pcas.json').subscribe(res => {
        // console.log(this.parse(res));
        // console.log(this.formatArea(res));
        this.http.get('assets/gangaotaiArea.json').subscribe(ress => {
          resolve(areaList.concat(this.parse(res), ress));
        });
      });
    });
  }
  private parse(obj) {
    const list = [];
    if (typeof obj === 'object') {
      for (const key in obj) {
        if (obj.hasOwnProperty(key)) {
          const element = obj[key];
          const provinceNew = key === '北京市' || key === '天津市' || key === '上海市' || key === '重庆市' ? key.split('市')[0] : key;
          const area: any = { value: provinceNew, label: provinceNew };
          if (Object.keys(element).length > 0 && typeof element === 'object') {
            area.children = [];
            if (element instanceof Array) {
              element.forEach(item => {
                area.children.push({ value: item, label: item, isLeaf: true });
              });
            } else if (element instanceof Object) {
              for (const itemKey in element) {
                if (element.hasOwnProperty(itemKey)) {
                  const itemElement = element[itemKey];
                  const cityNew = itemKey === '市辖区' ? key : itemKey;
                  area.children.push({ value: cityNew, label: cityNew, children: this.parse(itemElement) });
                }
              }
            }
          } else {
            area.isLeaf = true;
          }
          list.push(area);
        }
      }
    }
    return list;
  }
  private formatArea(obj) {
    const data: any = [];
    function areaFn(area) {
      if (typeof area === 'object') {
        for (const key in area) {
          if (area.hasOwnProperty(key)) {
            const element = area[key];
            const children = [];
            if (typeof element === 'object') {
              for (const childrenKey1 in element) {
                if (element.hasOwnProperty(childrenKey1)) {
                  const element1 = element[childrenKey1];
                  const children1 = [];
                  if (typeof element1 === 'object') {
                    for (const childrenKey2 in element1) {
                      if (element1.hasOwnProperty(childrenKey2)) {
                        const element2 = element1[childrenKey2];
                        const children2 = [];
                        if (typeof element2 === 'object') {
                          for (const childrenKey3 in element2) {
                            if (element2.hasOwnProperty(childrenKey3)) {
                              const element3 = element2[childrenKey3];
                              const children3 = [];
                              if (typeof element3 === 'string') {
                                children2.push({ label: element3, value: element3, isLeaf: true });
                              } else if (typeof element3 === 'object') {
                                children2.push({ label: element3, value: element3, children: [] });
                              }
                            }
                          }
                          children1.push({ label: childrenKey2, value: childrenKey2, children: children2 });
                        }
                      }
                    }
                    let cityNew = '';
                    if (childrenKey1 === '市辖区') {
                      cityNew = key;
                    } else {
                      cityNew = childrenKey1;
                    }
                    children.push({ label: cityNew, value: cityNew, children: children1 });
                  }
                }
              }
              data.push({ label: key, value: key, children: children });
            }
          }
        }
      }
    }
    areaFn(obj);
    return data;
  }
}
